import unittest
from tedtree import TEDTree
from node import Node
from tree import Tree
from compare import Compare

class TestTEDTree(unittest.TestCase):

    def setUp(self):
        root = Node("root", style=Node.ROOT)
        nodeA = Node("A", root, style=Node.NORM)
        nodeK = Node("K", nodeA, style=Node.NORM)
        nodeB = Node("B", nodeK, style=Node.NORM)
        nodeE = Node("E", nodeB, style=Node.NORM)
        nodeD = Node("D", nodeB, style=Node.NORM)
        nodeG = Node("G", nodeD, style=Node.NORM)
        root.setChild1(nodeA)
        nodeA.setChild1(nodeK)
        nodeK.setChild1(nodeB)
        nodeB.setChild1(nodeE)
        nodeB.setChild2(nodeD)
        nodeD.setChild2(nodeG)
        self.tree1 = Tree(root)
        root = Node("root", style=Node.ROOT)
        nodeA = Node("A", root, style=Node.NORM)
        nodeK = Node("K", nodeA, style=Node.NORM)
        nodeG = Node("G", nodeA, style=Node.NORM)
        nodeH = Node("H", nodeA, style=Node.NORM)
        nodeC = Node("C", nodeG, style=Node.NORM)
        nodeB = Node("B", nodeC, style=Node.NORM)
        root.setChild1(nodeA)
        nodeA.addChild(nodeK)
        nodeA.addChild(nodeG)
        nodeA.addChild(nodeH)
        nodeG.addChild(nodeC)
        nodeC.addChild(nodeB)
        self.tree2 = Tree(root)

    def test_parse_binary(self):
        ted = TEDTree("test")
        ted.parseBinaryTree("parse01F.txt")
        tree = ted.getTree()
        print tree.toString()
        compare = Compare(self.tree1, tree)
        self.assertTrue(compare.compareBinary())
        return

    def test_parse_non_binary(self):
        return

    def test_generate_binary(self):
        ted = TEDTree("Test")
        ted.createBinaryTree(self.tree1, "generate01F.txt")
        fp = open("generate01F.txt", "r")
        tree = fp.read()
        fp.close()
        print tree
        self.assertTrue(tree == "(A(K(B(E)(D(G)))))")
        return

    def test_generate_non_binary(self):
        ted = TEDTree("Test")
        ted.createNonBinaryTree(self.tree2, "generate01G.txt")
        fp = open("generate01G.txt", "r")
        tree = fp.read()
        fp.close()
        print tree
        self.assertTrue(tree == "(A(K)(G(C(B)))(H))")
        return

    def test_parse_results(self):
        ted = TEDTree("Test")
        results = ted.parseResults("Trees\Results\\results.txt")
        items = range(0,20)
        for key in results.keys():
            self.assertTrue(int(key) in items)
        self.assertTrue(results["0"] == {"DMRW":11, "Klien":11, "Zhang":11})

if __name__ == "__main__":
    unittest.main()
